function fun1(cb) {
    setTimeout(() => {
        console.log('1');
        cb();
    }, 300);
}

function fun2(cb) {
    setTimeout(() => {
        console.log('2');
        cb();
    }, 250);
}



function fun3(cb) {
    setTimeout(() => {
        console.log('3');
        cb();
    }, 150);
}



function queue(list, count) {

    let runTaskNum = 0;

    function run(task, next) {
        if (!task) {
            return null;
        }
        task(next);
    }

    function step(index) {
        for (let i = 0; i < count; i++) {
            if (i > count) {
                return;
            }
            const task = list[index + i];
            run(task, () => {
                runTaskNum++;
                if (runTaskNum >= count) {
                    step(index + runTaskNum);
                    runTaskNum = 0;
                }
            })
        }
    }

    step(0);
}



queue([fun1, fun2, fun3], 2);   // 依次输出 2、1、3

